<?php
/**
 * Created by PhpStorm.
 * User: Frings
 * Date: 2016/9/8
 * Time: 17:47
 */

use yii\helpers\Url;
$web = Url::base();
$this->title = '销售名片管理';
?>

<script type="application/javascript">
    function filter() {
        var saler = $("#f_saler").val();
        var name = $("#f_name").val();
        var voucher = $("#f_voucher").val();
        var startTime = $("#f_startTime").val();
        var endTime = $("#f_endTime").val();

        var href = window.location.href;
        var index = href.indexOf("?page=");
        if (index > 0) {
            href = href.substr(0, index);
        }
        var href_str = href + "?page=1";

        if (saler != "" && saler != null) {
            href_str = href_str + "&saler=" + saler;
        }
        if (name != "" && name != null) {
            href_str = href_str + "&name=" + name;
        }
        if (voucher != "" && voucher != null) {
            href_str = href_str + "&voucher=" + voucher;
        }
        if (startTime != "" && startTime != null) {
            href_str = href_str + "&startTime=" + startTime;
        }
        if (endTime != "" && endTime != null) {
            href_str = href_str + "&endTime=" + endTime;
        }

        window.location.href = href_str;
    }
    function onAddData() {
        $("#myModalEdit").modal('show');

        $("#modaledit").html('');
        requestHtml('card-sale-part-view', null, true, function (responseHtml) {
            $("#modaledit").html(responseHtml);
        });
    }

    function saveData() {
        var saler = $("#saler").val();
        var name = $("#name").val();
        var description = $("#description").val();
        var start_index = $("#start_index").val();
        var end_index = $("#end_index").val();

        if (saler == '' || saler == null) {
            $("#hint_message").text('销售人员不能为空！');
            $("#hintMessageModal").modal('show');
            return false;
        }
        if (name == '' || name == null) {
            $("#hint_message").text('名片名称不能为空！');
            $("#hintMessageModal").modal('show');
            return false;
        }
        if (description == '' || description == null) {
            $("#hint_message").text('名片描述不能为空！');
            $("#hintMessageModal").modal('show');
            return false;
        }
        if (start_index == '' || start_index == null) {
            $("#hint_message").text('单证开始不能为空！');
            $("#hintMessageModal").modal('show');
            return false;
        }
        if (end_index == '' || end_index == null) {
            $("#hint_message").text('单证结束不能为空！');
            $("#hintMessageModal").modal('show');
            return false;
        }
        if (parseInt(end_index) < parseInt(start_index)) {
            $("#hint_message").text('单证结束不能小于单证开始！');
            $("#hintMessageModal").modal('show');
            return false;
        }

        var data = {};
        data.saler = saler;
        data.name = name;
        data.description = description;
        data.start_index = start_index;
        data.end_index = end_index;

        loading.show();
        requestJson("card-sale-save-handler", data, true, function (response) {
            loading.hide();
            if (response.status == 0) {
                filter();
            } else {
                $("#hint_message").text(response.message);
                $("#hintMessageModal").modal('show');
            }
        });
    }

    function delData(id) {
        var data = {};
        data.id = id;
        loading.show();
        requestJson("card-sale-del-handler", data, true, function (response) {
            loading.hide();
            if (response.status == 0) {
                filter();
            } else {
                $("#hint_message").text(response.message);
                $("#hintMessageModal").modal('show');
            }
        });
    }

    //打开卡片单证
    function getCardVoucher(start_index, end_index) {
        $("#myModalCardVoucher").modal('show');
        var data = {};
        data.start_index = start_index;
        data.end_index = end_index;
        $("#modalCardVoucher").html('');
        requestHtml('card-sale-part-view-voucher', data, true, function (responseHtml) {
            $("#modalCardVoucher").html(responseHtml);
        });
    }
</script>

<style type="text/css">
    .modal-body {
        overflow: auto;
        max-height: 600px;
    }
</style>

<!-- head filter-->
<div class="panel panel-default">
    <div class="panel-body">
        <div class="form-inline">
            <div class="btn-group col-lg-2">
                <button class="btn btn-primary" type="button" onclick="onAddData()"><i class="fa fa-plus"></i> 新增
                </button>
            </div>
            <div class="pull-right col-lg-10">
                <div class="col-lg-2">
                    <input type="text" class="form-control" id="f_saler" placeholder="销售人员" value="<?= $saler ?>">
                </div>
                <div class="col-lg-2">
                    <input type="text" class="form-control" id="f_name" placeholder="销售名片" value="<?= $name ?>">
                </div>
                <div class="col-lg-2">
                    <input type="text" class="form-control" id="f_voucher" placeholder="单证号" value="<?= $voucher ?>">
                </div>

                <div class="col-lg-2">
                    <div data-date="" class="input-group date form_date">
                        <input type="text" class="form-control" readonly="" size="16" id="f_startTime"
                               name="f_startTime" placeholder="开始时间"
                               value="<?= $startTime ?>">
                        <div class="input-group-btn">
                            <button type="button" class="btn btn-default date-reset"><i class="fa fa-times"></i>
                            </button>
                            <button type="button" class="btn btn-default date-set"><i class="fa fa-calendar"></i>
                            </button>
                        </div>
                    </div>
                </div>

                <div class="col-lg-2">
                    <div data-date="" class="input-group date form_date">
                        <input type="text" class="form-control" readonly="" size="16" id="f_endTime"
                               name="f_endTime" placeholder="结束时间"
                               value="<?= $endTime ?>">
                        <div class="input-group-btn">
                            <button type="button" class="btn btn-default date-reset"><i class="fa fa-times"></i>
                            </button>
                            <button type="button" class="btn btn-default date-set"><i class="fa fa-calendar"></i>
                            </button>
                        </div>
                    </div>
                </div>

                <div class="col-lg-1">
                    <button type="button" class="btn btn-primary" onclick="filter()"><i class="fa fa-search"></i> 搜索
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- body table-->
<div class="panel panel-default" id="card_sale_list">
    <div class="panel-body">
        <div class="table-responsive">
            <table class="table table-bordered table-hover table-condensed">
                <thead class="nowrap">
                <tr>
                    <th>销售人员</th>
                    <th>名称</th>
                    <th>描述</th>
                    <th>单证号开始</th>
                    <th>单证号结束</th>
                    <th>创建人</th>
                    <th>创建时间</th>
                    <th>单证统计</th>
                    <th></th>
                </tr>
                </thead>
                <tbody>
                <?php
                if ($list != null && count($list) > 0) {
                    for ($i = 0; $i < count($list); $i++) {
                        ?>
                        <tr>
                            <td>
                                <?= $list[$i]['saler'] ?>
                            </td>
                            <td>
                                <?= $list[$i]['name'] ?>
                            </td>
                            <td>
                                <?= $list[$i]['description'] ?>
                            </td>
                            <td>
                                <?= $list[$i]['start_index'] ?>
                            </td>
                            <td>
                                <?= $list[$i]['end_index'] ?>
                            </td>
                            <td>
                                <?= $list[$i]['create_manager_name'] ?>
                            </td>
                            <td>
                                <?= date('Y-m-d', strtotime($list[$i]['create_datetime'])) ?>
                            </td>
                            <td>
                                <label
                                    class="label label-disable">总数:<?= $list[$i]['end_index'] - $list[$i]['start_index'] + 1 ?></label>
                                <label class="label label-info">已绑定:<?= $list[$i]['s1'] ?></label>
                                <label class="label label-success">已激活:<?= $list[$i]['s2'] ?></label>
                                <label class="label label-danger">已使用:<?= $list[$i]['s3'] ?></label>
                                <label class="label label-warning">失效:<?= $list[$i]['s4'] ?></label>
                                <label class="label label-warning">过期:<?= $list[$i]['s5'] ?></label>
                            </td>
                            <td>
                                <div class="btn-group">
                                    <button class="btn btn-link btn-xs" type="button"
                                            onclick="getCardVoucher('<?= $list[$i]['start_index'] ?>','<?= $list[$i]['end_index'] ?>')"
                                            data-toggle="modal"
                                            data-target="#myModalCardVoucher">
                                        单证
                                    </button>
                                    <button class="btn btn-link btn-xs" type="button"
                                            onclick="delData('<?= $list[$i]['id'] ?>')">
                                        删除
                                    </button>
                                </div>
                            </td>
                        </tr>
                        <?php
                    }
                }
                ?>
                </tbody>
            </table>
        </div>
        <div class="pull-left">
            <?= \app\widgets\LinkPagerExt::widget(['pagination' => $pagination]) ?>
        </div>
    </div>
</div>

<!-- Modal card Edit-->
<div class="modal fade" id="myModalEdit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">销售名片</h4>
            </div>
            <div id="modaledit" class="modal-body">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="saveData()"><i class="fa fa-save"></i> 保存
                </button>
                <button type="button" class="btn btn-primary" data-dismiss="modal"><i class="fa fa-remove"></i> 取消
                </button>
            </div>
        </div>
    </div>
</div>

<!--删除确认对话框-->
<div aria-hidden="true" role="dialog" tabindex="-1" id="deleteModal" class="modal fade">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">是否确认操作？</h4>
                <input type="hidden" id="deleteDataId" class="form-control" value="">
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary" onclick="delData()"><i class="fa fa-ok"></i> 确认</button>
                <button class="btn btn-primary" onclick="delClose()"><i class="fa fa-remove"></i> 取消</button>
            </div>
        </div>
    </div>
</div>

<!-- card voucher-->
<div class="modal fade" id="myModalCardVoucher" tabindex="-1" role="dialog" aria-labelledby="myModalCardVoucherLable">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalCardVoucherLable">单证</h4>
            </div>
            <div id="modalCardVoucher" class="modal-body">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-dismiss="modal"><i class="fa fa-remove"></i> 取消
                </button>
            </div>
        </div>
    </div>
</div>


<!--提示对话框-->
<div aria-hidden="true" role="dialog" tabindex="-1" id="hintMessageModal" class="modal fade">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">提示</h4>
            </div>
            <div class="modal-body" id="hint_message" align="center">
            </div>
        </div>
    </div>
</div>

<link rel="stylesheet" type="text/css" href="<?= $web ?>/css/datetimepicker-custom.css"/>
<script src="<?= $web ?>/js/bootstrap-datetimepicker.js"></script>
<script src="<?= $web ?>/js/pickers-init.js"></script>